package com.mcflies.ui.listView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;

import com.mcflies.commons.McfliesCommonsUtil;
import com.mcflies.commons.McfliesConstant;
import com.mcflies.commons.MenuMainAdapter;
import com.mcflies.ui.R;

/**
 * @author:mcfly_jun
 * @create_date:2012-8-15
 * @comment:ListView总结--GridView菜单界面
 */
public class ListViewMainiActivity extends Activity implements
		OnItemClickListener {
	private GridView gridView;
	private AssetManager assetManager;
	private String[] listViewMainArray;
	private List<Map<String, Object>> listViewMainList = new ArrayList<Map<String, Object>>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu_main);
		initWidget();
		initData();
		gridView.setAdapter(new MenuMainAdapter(this, listViewMainList));
		gridView.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> adapter, View view, int position,
			long id) {
		try {
			Map<String, Object> item = listViewMainList.get(position);
			String itemIntent = (String) item
					.get(McfliesConstant.MenuMainConstant.MENU_MAIN_INTENT);
			Intent intent = new Intent(ListViewMainiActivity.this,
					Class.forName(itemIntent));
			startActivity(intent);
		} catch (Exception e) {
			Log.i(McfliesConstant.MCFLIES_EXCEPTION_INFO_FLAG, e.getMessage());
		}
	}

	private void initData() {
		listViewMainArray = getResources().getStringArray(
				R.array.listViewMainArray);
		assetManager = getAssets();
		listViewMainList = McfliesCommonsUtil.assembleMenuDataList(
				listViewMainArray, assetManager, "listViewMain");
	}

	private void initWidget() {
		gridView = (GridView) findViewById(R.id.menuMainGridView);
	}
}
